import json
from datetime import datetime

from OpenSSL.rand import status
from django.forms import model_to_dict
from django.http import JsonResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from scripts.regsetup import description

from zuoye20250917.models import student


# Create your views here.
@csrf_exempt
def post_student(request):
    data=json.loads(request.body)
    if data['name']=='' or data['age']=='' or data['gender'] == '':
        return JsonResponse(
            {
                'msg':'该参数不能为空'
            },status=400
        )
    if student.objects.filter(name=data['name']).exists():
        return JsonResponse(
            {
                'msg':'该姓名已存在'
            },status=404
        )
    a=student.objects.create(
        name=data['name'],
        age=data['age'],
        gender=data['gender'],
        deadline=datetime.now(),
        description=data['description']
    )
    return JsonResponse(
        {
            'msg':'注册成功',
            'item': model_to_dict(a)
        }
    )
def get_student(request,id=None):

    if id==None:
        return JsonResponse(
            {
                'msg':'id必填'
            },status=10
        )
    data=student.objects.get(id=id)
    return JsonResponse(
        {
            'msg':'查询成功',
            'item':model_to_dict(data)
        }
    )
def get_student_list(request):
    data = student.objects.all()
    name = request.GET.get('name')
    gender = request.GET.get('gender')
    if name:
        data=data.filter(name=name)
    if gender:
        data=data.filter(gender=gender)
    l=[]
    for i in data:
        l.append(
            {
                'id':i.id,
                'name':i.name,
                'gender':i.gender,
                'deadline':i.deadline,
                'description':i.description
            }
        )
    return JsonResponse(
        {
            'msg':'查询成功',
            'item':l
        }
    )
@csrf_exempt
def put_student(request,id=None):
    data=json.loads(request.body)
    if id==None:
        return JsonResponse(
            {
                'msg':'id必填'
            },status=400
        )
    if data['name']=='':
        return JsonResponse(
            {
                'msg':'请输入学生姓名'
            }
        )
    u=student.objects.get(name=data['name'])
    u.age=data['age']
    u.save()
    return JsonResponse(
        {
           'msg':'修改成功',
            'item':model_to_dict(u)
        }
    )
@csrf_exempt
def delete_student(request,id=None):
    if id==None:
        return JsonResponse(
            {
                'msg':'id必填'
            },status=400
        )
    student.objects.get(id=id).delete()
    return JsonResponse(
        {
            'msg':'删除成功'
        }
    )
@csrf_exempt
def delete_post(request):
    data=json.loads(request.body)
    if data['id']=='':
        return JsonResponse(
            {
                'msg':'id必填'
            }
        )
    student.objects.get(id=data['id']).delete()
    return JsonResponse(
        {
            'msg':'删除成功'
        }
    )